package com.nanhai.buddhist.college.module.collegebase.service.studentrewardpunish;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil;
import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.util.collection.CollectionUtils;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentrewardpunish.vo.StudentRewardPunishCreateReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentrewardpunish.vo.StudentRewardPunishExportReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentrewardpunish.vo.StudentRewardPunishPageReqVO;
import com.nanhai.buddhist.college.module.collegebase.controller.admin.studentrewardpunish.vo.StudentRewardPunishUpdateReqVO;
import com.nanhai.buddhist.college.module.collegebase.convert.studentrewardpunish.StudentRewardPunishConvert;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.studentrewardpunish.StudentRewardPunishDO;
import com.nanhai.buddhist.college.module.collegebase.dal.mysql.studentrewardpunish.StudentRewardPunishMapper;
import com.nanhai.buddhist.college.module.collegebase.enums.ErrorCodeConstants;
import org.springframework.stereotype.Service;
import static com.nanhai.buddhist.college.framework.common.exception.util.ServiceExceptionUtil.exception;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.Collections;
import java.util.List;



/**
 * 奖励惩罚表 Service 实现类
 *
 * @author AKai
 */
@Service
@Validated
public class StudentRewardPunishServiceImpl implements StudentRewardPunishService {

    @Resource
    private StudentRewardPunishMapper studentRewardPunishMapper;

    @Override
    public String createStudentRewardPunish(StudentRewardPunishCreateReqVO createReqVO) {
        // 插入
        StudentRewardPunishDO studentRewardPunish = StudentRewardPunishConvert.INSTANCE.convert(createReqVO);
        studentRewardPunishMapper.insert(studentRewardPunish);
        // 返回
        return studentRewardPunish.getId();
    }

    @Override
    public void updateStudentRewardPunish(StudentRewardPunishUpdateReqVO updateReqVO) {
        // 校验存在
        this.validateStudentRewardPunishExists(updateReqVO.getId());
        // 更新
        StudentRewardPunishDO updateObj = StudentRewardPunishConvert.INSTANCE.convert(updateReqVO);
        studentRewardPunishMapper.updateById(updateObj);
    }

    @Override
    public void deleteStudentRewardPunish(String id) {
        // 校验存在
        this.validateStudentRewardPunishExists(id);
        // 删除
        studentRewardPunishMapper.deleteById(id);
    }

    private void validateStudentRewardPunishExists(String id) {
        if (studentRewardPunishMapper.selectById(id) == null) {
            throw ServiceExceptionUtil.exception(ErrorCodeConstants.STUDENT_REWARD_PUNISH_NOT_EXISTS);
        }
    }

    @Override
    public StudentRewardPunishDO getStudentRewardPunish(String id) {
        return studentRewardPunishMapper.selectById(id);
    }

    @Override
    public List<StudentRewardPunishDO> getStudentRewardPunishList(Collection<String> ids) {
        return CollectionUtils.isEmpty(ids) ? Collections.emptyList() : studentRewardPunishMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<StudentRewardPunishDO> getStudentRewardPunishPage(StudentRewardPunishPageReqVO pageReqVO) {
        return studentRewardPunishMapper.selectPage(pageReqVO);
    }

    @Override
    public List<StudentRewardPunishDO> getStudentRewardPunishList(StudentRewardPunishExportReqVO exportReqVO) {
        return studentRewardPunishMapper.selectList(exportReqVO);
    }

    @Override
    public List<StudentRewardPunishDO> getStudentRewardPunishAllList(String type) {
        return studentRewardPunishMapper.selectList(new LambdaQueryWrapper<StudentRewardPunishDO>().eq(StudentRewardPunishDO::getType,type));
    }

}
